<?php

/**
 * Accesses the tgp_grid_boxes table in the thegridpaper database
 * @author panosbaroudjian
 *
 */
class Application_Model_DbTable_GridBoxes extends Zend_Db_Table_Abstract
{
    protected $_name = 'tgp_grid_boxes';
    protected $_primary = array('account_id', 'feed_entry_id');
    protected $_referenceMap = array(
                                    'Account' => array('columns' => array('account_id'),
                                                    'refTableClass' => 'Application_Model_DbTable_Accounts',
                                                    'refColumns' => array('account_id'),
                                                    'onDelete' => self::CASCADE),
                                    'FeedEntry' => array('columns' => array('feed_entry_id'),
                                                        'refTableClass' => 'Application_Model_DbTable_FeedEntries',
                                                        'refColumns' => array('feed_entry_id'),
                                                        'onDelete' => self::CASCADE));

    /**
     * Gets a GridBox by its primary keys
     * @param int $accountId The id of the account
     * @param int $feedEntryId The id of the feed entry
     * @return Application_Model_GridBox The gridbox
     */
    public function getById($accountId, $feedEntryId)
    {
        $accountId = (int)$accountId;
        $feedEntryId = (int)$feedEntryId;
        $where = array();
        $where['account_id = ?'] = $accountId;
        $where['feed_entry_id = ?'] = $feedEntryId;
        $row = $this->fetchRow($where);
        $feedEntryRow = $row->findParentRow('Application_Model_DbTable_FeedEntries');
        $gridBox = Application_Model_GridBox::fromTableRow($row->toArray());
        $gridBox->feed_entry = Application_Model_FeedEntry::fromTableRow($feedEntryRow->toArray());
        return $gridBox;
    }

    /**
     * Sets the read/unread status of a gridbox
     * @param int $accountId The id of the account
     * @param int $feedEntryId The id of the feed
     * @param bool $isRead If the gridbox is read or not
     */
    public function setIsRead($accountId, $feedEntryId, $isRead)
    {
        $accountId = (int)$accountId;
        $feedEntryId = (int)$feedEntryId;
        $where = array();
        $where['account_id = ?'] = $accountId;
        $where['feed_entry_id = ?'] = $feedEntryId;
        $data = array('isRead' => $isRead);
        $this->update($data, $where);
    }

    /**
     * Get gridboxes of an account
     * @param int $accountId The id of the account
     * @return NULL|array: An array containing the rows if found, NULL otherwise.
     */
    public function getByAccountId($accountId)
    {
        $select = $this->select();
        $select->where('account_id = ?', (int)$accountId);
        $rows = $this->fetchAll($select);
        if (!$rows)
            return null;
        $result = array();
        foreach ($rows as $row)
        {
            $current = $row;
            $feed_entry_row = $current->findParentRow('Application_Model_DbTable_FeedEntries');
            $gridBox = Application_Model_GridBox::fromTableRow($row->toArray());
            $gridBox->feed_entry = Application_Model_FeedEntry::fromTableRow($feed_entry_row->toArray());
            array_push($result, $gridBox);
        }
        return $result;
    }

    /**
     * Gets the number of read gridboxes of an account
     * @param int $accountId The id of the account
     * @return int: The number of read gridboxes
     */
    public function getNumberOfReadById($accountId)
    {
        $where = array();
        $where['account_id = ?'] = (int)$accountId;
        $where['isRead = ?'] = true;
        $rows = $this->fetchAll($where);
        return $rows->count();
    }

    /**
     * Inserts a new gridbox
     * @param unknown_type $account_id The id of the account
     * @param unknown_type $feed_entry_id The id of the feed entry
     * @param unknown_type $left The left position of the gridbox
     * @param unknown_type $top The top position of the gridbox
     * @param unknown_type $width The width of the gridbox
     * @param unknown_type $height The height of the gridbox
     * @return int: The id of the newly inserted gridbox
     */
    public function insertGridBox($account_id, $feed_entry_id, $left, $top, $width, $height)
    {
        $data = array('account_id' => $account_id,
                    'feed_entry_id' => $feed_entry_id,
                    'left' => $left,
                    'top' => $top,
                    'width' => $width,
                    'height' => $height);
        return $this->insert($data);
    }

    /**
     * Deletes all the gridboxes of an account
     * @param int $accountId The id of the account
     */
    public function deleteByAccountId($accountId)
    {
        $where = $this->getAdapter()->quoteInto('account_id = ?', $accountId);
        $this->delete($where);
    }
}

